package com.study7.springcache.service.impl;

import com.study7.springcache.mapper.CarrierCodeMapper;
import com.study7.springcache.model.CarrierCode;
import com.study7.springcache.service.CarrierCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xuly
 * @title: CarrierCodeServiceImpl
 * @projectName spring-boot
 * @description: TODO
 * @date 2019-07-2411:35
 **/
@Service("carrierCodeService")
//相当于@Cacheable(value = "models"
@CacheConfig(cacheNames = "models")
@Slf4j
public class CarrierCodeServiceImpl implements CarrierCodeService {

    @Resource
    private CarrierCodeMapper carrierCodeMapper;

    @Override
    @Cacheable(value = "models", key = "'getById' + #id",condition = "#id !=  null ")
    public CarrierCode getById(Integer id) {
        log.info(">>>>>>>>>>>>>");
        return carrierCodeMapper.getById(id);
    }

    @Override
    @CacheEvict(value = "models", key = "'getById' + #id",condition = "#id !=  null ")
    public void deleteId(Integer id) {

    }

    @Override
    @CachePut(value = "models", key = "'getById' + #id",condition = "#id !=  null ")
    public CarrierCode update(Integer id) {
        CarrierCode byId = carrierCodeMapper.getById(id);
        byId.setCarrierCode("test");
        return byId;
    }

}
